var GoodsClass = function() {

	var left_margin_step = 15;
	var intSelProdInfoLen = 0;
	var intChangeLineCount = 48;
	//"[{"name":"水果","pk_marbasclass":"1001ZF10000000003DX1"},{"name":"书籍","code":"22"}]"
	$("#qrygoodsClass").bind(
			'click',
			function() {
				var myts = new Date();
				var formStr = $('#prodclass_filter_form').serialize();
				jQuery.ajax( {
					type : "POST",
					async : false,
					url : ebvp_context + "/other/qryMarClass?myts=" + myts,
					dataType : "text",
					data : "&" + formStr,
					success : function(result) {
					//"[{"name":"水果","pk_marbasclass":"1001ZF10000000003DX1"},{"name":"书籍","code":"22"}]"
					var chulifenlei = "[]";
					if($("#chulifenlei").val() != null && $("#chulifenlei").val() !=""){
						chulifenlei = $("#chulifenlei").val();
					}
					result = chulifenlei;
						if(result != "[]"){
							var orgArr = eval(result);
							$("#productUnitPicker").find(".tree_list1").empty();
							for ( var loop = 0; loop < orgArr.length; loop++) {
								var obj = orgArr[loop];
								var prodClassPk = obj.pk_marbasclass;
								var prodClassName = obj.name;
								var a = $("<a></a>").attr("href", "#").addClass(
										"category_trigger1").attr("margin", 0);
								var inputObj = $("<input/>")
												.attr("type", "checkbox")
												.attr("id", "pk_marbasclass_intree")
												.attr("name", "pk_marbasclass_intree")
												.attr("style", "height:13px;width:13px")
												.val(obj.pk_marbasclass+"_"+obj.name)
												.addClass(obj.pk_marbasclass);
								$(inputObj).click(function(){
									var checkValue = $(this).val();
									var checkProdClassPk = checkValue.substr(0,20);
									var checkProdClassName = checkValue.substr(21);
									if(this.checked){
										var checkBox = $("<input/>")
														.attr("type", "checkbox")
														.val(checkValue)
														.attr("style","margin-left:5px;px;margin-top:5px").attr("checked","checked").attr("style", "height:13px;width:13px");
										$(checkBox).click(function(){
											var checkValue = $(this).val();
											var checkProdClassPk = checkValue.substr(0,20);
											var checkProdClassName = checkValue.substr(21);
											var objs = $("."+checkProdClassPk);
											if(objs.length > 0){
												objs[0].checked = false;
											} 
											$(this).parent().remove();
											intSelProdInfoLen = intSelProdInfoLen - len(prodClassName);
											//设置已选产品去高度
											$(".ui-drop_dialog_content").attr("style","height:"+ (336 +(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26)+"px");
											$(".border.lft").attr("style","height:"+ (332 +(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26)+"px");
											$(".border.rit").attr("style","height:"+ (332 +(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26)+"px");
											$("#sel_prodclass").attr("style","height:"+(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26+"px");
										});
										var labelProd = $("<label />").html(checkProdClassName)
										var div = $("<div></div>").attr("id", checkProdClassPk).attr("name", checkProdClassPk).attr("style", "float:left").append(checkBox).append(labelProd);
										//判断已经物料的宽度
										intSelProdInfoLen = intSelProdInfoLen + len(checkProdClassName);
										$("#sel_prodclass").append(div);;
									}else{
										intSelProdInfoLen = intSelProdInfoLen - len(checkProdClassName);
										$("#"+checkProdClassPk).remove();
									}
									//设置已选产品去高度
									$(".ui-drop_dialog_content").attr("style","height:"+ (336 +(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26)+"px");
									$(".border.lft").attr("style","height:"+ (332 +(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26)+"px");
									$(".border.rit").attr("style","height:"+ (332 +(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26)+"px");
									$("#sel_prodclass").attr("style","height:"+(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26+"px");
								});
								
								// chuliwulfenlei
								if($("#wulflvalueai").val() != null && $("#wulflvalueai").val() !=""){
									var pk_marbasclassvalu = $("#wulflvalueai").val();
									var qrygoodsClassvau = $("#wulflvalueainame").val();
									var pk_marbasclassvalunew = pk_marbasclassvalu.split(",");
									var qrygoodsCldww = qrygoodsClassvau.split(",");
									if(pk_marbasclassvalunew.length == 1 && pk_marbasclassvalunew[0] == ""){
										
									} else {
										for ( var rowsLoogp = 0; rowsLoogp < pk_marbasclassvalunew.length; rowsLoogp++) {
											var pk_marbasclvaelua1 = pk_marbasclassvalunew[rowsLoogp];
											var qrygoodsCldwwsa = qrygoodsCldww[rowsLoogp];
											if(prodClassPk == pk_marbasclvaelua1){
												var checkValue = 	pk_marbasclvaelua1 + "_" +qrygoodsCldwwsa;
												var checkBox = $("<input/>")
												.attr("type", "checkbox")
												.val(checkValue)
												.attr("style","margin-left:5px;px;margin-top:5px").attr("checked","checked").attr("style", "height:13px;width:13px");
												$(checkBox).click(function(){
													var checkValue = $(this).val();
													var checkProdClassPk = checkValue.substr(0,20);
													var checkProdClassName = checkValue.substr(21);
													var objs = $("."+checkProdClassPk);
													if(objs.length > 0){
														objs[0].checked = false;
													} 
													$(this).parent().remove();
													intSelProdInfoLen = intSelProdInfoLen - len(prodClassName);
													//设置已选产品去高度
													$(".ui-drop_dialog_content").attr("style","height:"+ (336 +(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26)+"px");
													$(".border.lft").attr("style","height:"+ (332 +(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26)+"px");
													$(".border.rit").attr("style","height:"+ (332 +(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26)+"px");
													$("#sel_prodclass").attr("style","height:"+(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26+"px");
												});
												var labelProd = $("<label />").html(qrygoodsCldwwsa)
												var div = $("<div></div>").attr("id", pk_marbasclvaelua1).attr("name", pk_marbasclvaelua1).attr("style", "float:left").append(checkBox).append(labelProd);
												//判断已经物料的宽度
												intSelProdInfoLen = intSelProdInfoLen + len(qrygoodsCldwwsa);
												$("#sel_prodclass").append(div);
											}
										}
									}
								}
								
								//查询该物料是否已经被选择
								var objOrg = $("#"+obj.pk_marbasclass);
								if(objOrg.length > 0){
									inputObj.attr("checked", "checked");
								}
								var label = $("<label></label>").text(obj.name);
								var inputObj1 = $("<input/>").val(obj.pk_marbasclass).attr(
										"type", "hidden");	
								var div = $("<div></div>")
										.addClass("tree_category1").append(a)
										.append(inputObj).append(label).append(inputObj1);												
								$("#productUnitPicker").find(".tree_list1")
										.append(div);
							}
							$("#wulflvalueai").val("");
							$("#wulflvalueainame").val("");
						}
					}
				})
				$("#productUnitPicker").html();		
			});
	function pkpubclassai(){
		var wulflvalue = $("#pk_marbasclass").val();
		var newwulflvalue = wulflvalue.split(",");
		if(newwulflvalue.length == 1 && newwulflvalue[0] == ""){
			$(".data_amount_row").show();
			var datearowlength = $(".data_amount_row").length;
			var jishuqiwl = 0;
			for ( var rowsLoogp = 0; rowsLoogp < datearowlength; rowsLoogp++) {
				var wulnemavalue = $(".data_amount_row")[rowsLoogp];
				var stylevalue = $(wulnemavalue).attr("style");
				var displeyvalue = $(wulnemavalue).css("display");
				if("block" == displeyvalue){
					jishuqiwl ++;
				}
			}
			$(".color_f30000").text(jishuqiwl);
		} else {
			$(".data_amount_row").hide();
			for ( var rowsLoogp = 0; rowsLoogp < newwulflvalue.length; rowsLoogp++) {
				var wulnemavalue = newwulflvalue[rowsLoogp];
				var obj = $("body").find("[idfl='"+wulnemavalue+"']");
				obj.show();
			}
			var datearowlength = $(".data_amount_row").length;
			var jishuqiwl = 0;
			for ( var rowsLoogp = 0; rowsLoogp < datearowlength; rowsLoogp++) {
				var wulnemavalue = $(".data_amount_row")[rowsLoogp];
				var stylevalue = $(wulnemavalue).attr("style");
				var displeyvalue = $(wulnemavalue).css("display");
				if("block" == displeyvalue){
					jishuqiwl ++;
				}
			}
			$(".color_f30000").text(jishuqiwl);
		}
	}
	
	function pkpubclassje(){
		var shjhnumnumScale = 2;
		var listdcd = $("#listdcd").val();
		for ( var rowsLoogp = 0; rowsLoogp < new Number(listdcd); rowsLoogp++) {
			var resjevlzio = null;
			$("li[resjeid=resid"+rowsLoogp+"]").each(function(index, obj){
				var displeyvalue = $(obj).parents(".data_amount_row").css("display");
				var resjevllue = $(obj).attr("resjevl");
				var resjevlenglue = $(obj).attr("resjevleng");
				if("block" == displeyvalue && resjevllue != null &&　resjevllue　!= ""){
					if(resjevlzio == null){
						resjevlzio = 0.00
					}
					resjevlzio = new Number(resjevllue) + new Number(resjevlzio);
					if(resjevlenglue !=null && resjevlenglue!=""){
						shjhnumnumScale = resjevlenglue;
					}
				}
			});
			if(resjevlzio == null || resjevlzio == ""){
				$("span[resjebdid=resid"+rowsLoogp+"]").text("");
			} else {
				$("span[resjebdid=resid"+rowsLoogp+"]").text(subRedswiuNum(resjevlzio+"",shjhnumnumScale));
			}
		}
	}
	
	function subRedswiuNum(objvalue,intPrecision){
		var strValue = objvalue.replace(/(^\s*)|(\s*$)/g, "");
		if(isNaN(strValue)){
			return;
		}
		if(strValue == null || strValue == ""){
			return;
		}
		if (strValue.indexOf(".") != -1){
			var values = strValue.split(".");
			if(values[1].length > intPrecision){
				if(intPrecision > 0){
					strValue = values[0] + "." + values[1].substring(0,intPrecision);
					strValue = roundsdNum(strValue,values[1].substring(intPrecision),intPrecision);
				}else{
					strValue = values[0];
				}
			}else{
				for(var i=values[1].length;i<intPrecision;i++){
					strValue += "0";
				}
			}
		}else{
			if(intPrecision > 0){
				if(strValue.indexOf(".") == -1){
					strValue += ".";
					for(var i=0;i<intPrecision;i++){
						strValue += "0";
					}
				}
			}
		}
		return strValue;
	}
	function roundsdNum(oldValue,tailValue,intPrecision){
		var bAddOne = false;
		var numArray = new Array();
		for(var i=0;i<tailValue.length;i++){
			numArray[i] = tailValue.substring(i,i+1);
		}
		if(parseInt(numArray[0]) > 4){
			bAddOne = true;
		}else{
			for(var i=tailValue.length - 1;i>0;i--){
				if(parseInt(numArray[i]) > 4){
					numArray[i-1] = parseInt(numArray[i-1]) + 1;
				}
			}
			if(parseInt(numArray[0]) > 4){
				bAddOne = true;
			}
		}
		if(bAddOne){
			var strAddStr = "0.";
			for(var i=0;i<intPrecision;i++){
				if(i != intPrecision - 1){
					strAddStr = strAddStr + "0";
				}else{
					strAddStr = strAddStr + "1";
				}
			}
			oldValue = (parseFloat(oldValue) + parseFloat(strAddStr)) + "";
			if(oldValue.indexOf(".") != -1){
				var values = oldValue.split(".");
				oldValue = values[0] + "." + values[1].substring(0,intPrecision);
			} else {
				
				if(intPrecision > 0){
					oldValue += ".";
					for(var i=0;i<intPrecision;i++){
						oldValue += "0";
					}
				}
			}
		}
		return oldValue;
	}
	$("#filterProdClass").bind(
			'click',
			function() {
				var myts = new Date();
				var formStr = $('#prodclass_filter_form').serialize();
				jQuery.ajax( {
					type : "POST",
					async : false,
					url : ebvp_context + "/other/filterMarClass?myts=" + myts,
					dataType : "text",
					data : "&" + formStr,
					success : function(result) {
						if(result != "[]"){
							var orgArr = eval(result);
							$("#productUnitPicker").find(".tree_list1").empty();
							for ( var loop = 0; loop < orgArr.length; loop++) {
								var obj = orgArr[loop];
								var prodClassPk = obj.pk_marbasclass;
								var prodClassName = obj.name;
								var a = $("<a></a>").attr("href", "#").addClass(
										"category_trigger1").attr("margin", 0);								
								var inputObj = $("<input/>")
												.attr("type", "checkbox")
												.attr("id", "pk_marbasclass_intree")
												.attr("name", "pk_marbasclass_intree")
												.attr("style", "height:13px;width:13px")
												.val(obj.pk_marbasclass+"_"+obj.name)
												.addClass(obj.pk_marbasclass);
								$(inputObj).click(function(){
									var checkValue = $(this).val();
									var checkProdClassPk = checkValue.substr(0,20);
									var checkProdClassName = checkValue.substr(21);
									if(this.checked){
										var checkBox = $("<input/>")
														.attr("type", "checkbox")
														.val(checkValue)
														.attr("style","margin-left:5px;px;margin-top:5px").attr("checked","checked").attr("style", "height:13px;width:13px");
										$(checkBox).click(function(){
											var checkValue = $(this).val();
											var checkProdClassPk = checkValue.substr(0,20);
											var checkProdClassName = checkValue.substr(21);
											var objs = $("."+checkProdClassPk);
											if(objs.length > 0){
												objs[0].checked = false;
											}
											$(this).parent().remove();
											intSelProdInfoLen = intSelProdInfoLen - len(prodClassName);
											//设置已选产品去高度
											$(".ui-drop_dialog_content").attr("style","height:"+ (336 +(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26)+"px");
											$(".border.lft").attr("style","height:"+ (332 +(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26)+"px");
											$(".border.rit").attr("style","height:"+ (332 +(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26)+"px");
											$("#sel_prodclass").attr("style","height:"+(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26+"px");
										});
										var labelProd = $("<label />").html(checkProdClassName)
										var div = $("<div></div>").attr("id", checkProdClassPk).attr("name", checkProdClassPk).attr("style", "float:left").append(checkBox).append(labelProd);
										//判断已经物料的宽度
										intSelProdInfoLen = intSelProdInfoLen + len(checkProdClassName);
										$("#sel_prodclass").append(div);;
									}else{
										intSelProdInfoLen = intSelProdInfoLen - len(checkProdClassName);
										$("#"+checkProdClassPk).remove();
									}
									//设置已选产品去高度
									$(".ui-drop_dialog_content").attr("style","height:"+ (336 +(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26)+"px");
									$(".border.lft").attr("style","height:"+ (332 +(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26)+"px");
									$(".border.rit").attr("style","height:"+ (332 +(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26)+"px");
									$("#sel_prodclass").attr("style","height:"+(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26+"px");
								});
								var label = $("<label></label>").text(obj.name);
								var inputObj1 = $("<input/>").val(obj.pk_marbasclass).attr(
										"type", "hidden");	
								var div = $("<div></div>")
										.addClass("tree_category1").append(a)
										.append(inputObj).append(label).append(inputObj1);												
								$("#productUnitPicker").find(".tree_list1")
										.append(div);
							}
							
						}
					}
				})
				$("#productUnitPicker").html();				
			});
			
	$("#qrygoodsClass").UIDropDialog( {
		'triggerClass' : "idTreePicker",
		'content' : $("#productUnitPicker")
	});

	//"close"样式(-)是已经展开时的样式
	// 控制方法：
	// 如果当前节点尚未展开(+)，此次点击触发下列行为：(1)将当前节点样式设置为已展开；(2)从后台查数据，渲染子节点;(3)将同辈的其他已展开节点及其子节点收缩，且将收缩节点的样式设置为收缩样式
	// 如果当前节点已经展开(-)，此次点击触发下列行为：(1)将当前节点的子节点收缩；(2)当前节点的样式设置为展开(-)
	$(".category_trigger1").live(
			'click',
			function() {
				var category_triggerObj=$(this);
				if(category_triggerObj.hasClass("close")){
					category_triggerObj.parent(".tree_category1").next(".children").slideUp(function(){
						category_triggerObj.removeClass("close");
					});
					return false;
				}
				$(this).addClass("close");
				var pk_marbasclass = $(this).next("input").next().next().val();
				var myts = new Date();
				var formStr = $('#prodclass_filter_form').serialize();
				jQuery.ajax( {
					type : "POST",
					async : false,
					url : ebvp_context
							+ "/other/qryMarClassByPk?pk_marbasclass="
							+ pk_marbasclass 
							+ "&myts=" + myts,
					dataType : "text",
					data : "&" + formStr,
					success : function(result) {
						result = "[]"
						var orgArr = eval(result);
						var current_margin_left_size = parseInt(category_triggerObj.attr("margin")) + left_margin_step;	
						var childrenDiv = $("<div></div>").addClass("children");
						for ( var loop = 0; loop < orgArr.length; loop++) {
							var obj = orgArr[loop];
							var prodClassPk = obj.pk_marbasclass;
							var prodClassName = obj.name;
							var a = $("<a></a>").attr("href", "#")
									.addClass("category_trigger1")
									.attr("style","margin-left:"+current_margin_left_size+"px")
									.attr("margin", current_margin_left_size);
							var inputObj = $("<input/>")
											.attr("type", "checkbox")
											.attr("id", "pk_marbasclass_intree")
											.attr("name", "pk_marbasclass_intree")
											.attr("style", "height:13px;width:13px")
											.val(obj.pk_marbasclass+"_"+obj.name)
											.addClass(obj.pk_marbasclass);
							$(inputObj).click(function(){
								var checkValue = $(this).val();
								var checkProdClassPk = checkValue.substr(0,20);
								var checkProdClassName = checkValue.substr(21);
								if(this.checked){
									var checkBox = $("<input/>")
													.attr("type", "checkbox")
													.val(checkValue)
													.attr("style","margin-left:5px;px;margin-top:5px").attr("checked","checked").attr("style", "height:13px;width:13px");
									$(checkBox).click(function(){
										var checkValue = $(this).val();
										var checkProdClassPk = checkValue.substr(0,20);
										var checkProdClassName = checkValue.substr(21);
										var objs = $("."+checkProdClassPk);
										if(objs.length > 0){
											objs[0].checked = false;
										}
										$(this).parent().remove();
										intSelProdInfoLen = intSelProdInfoLen - len(prodClassName);
										//设置已选产品去高度
										$(".ui-drop_dialog_content").attr("style","height:"+ (336 +(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26)+"px");
										$(".border.lft").attr("style","height:"+ (332 +(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26)+"px");
										$(".border.rit").attr("style","height:"+ (332 +(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26)+"px");
										$("#sel_prodclass").attr("style","height:"+(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26+"px");
									});
									var labelProd = $("<label />").html(checkProdClassName)
									var div = $("<div></div>").attr("id", checkProdClassPk).attr("name", checkProdClassPk).attr("style", "float:left").append(checkBox).append(labelProd);
									//判断已经物料的宽度
									intSelProdInfoLen = intSelProdInfoLen + len(checkProdClassName);
									$("#sel_prodclass").append(div);;
								}else{
									intSelProdInfoLen = intSelProdInfoLen - len(checkProdClassName);
									$("#"+checkProdClassPk).remove();
								}
								//设置已选产品去高度
								$(".ui-drop_dialog_content").attr("style","height:"+ (336 +(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26)+"px");
								$(".border.lft").attr("style","height:"+ (332 +(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26)+"px");
								$(".border.rit").attr("style","height:"+ (332 +(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26)+"px");
								$("#sel_prodclass").attr("style","height:"+(parseInt(intSelProdInfoLen / intChangeLineCount)+1)*26+"px");
							});
							//查询该物料是否已经被选择
							var objProdClass = $("#"+obj.pk_marbasclass);
							if(objProdClass.length > 0){
								inputObj.attr("checked", "checked");
							}
							var label = $("<label></label>").text(obj.name);
							var inputObj1 = $("<input/>")
											.val(obj.pk_marbasclass)
											.attr("type", "hidden");	
							var div = $("<div></div>")
									.addClass("tree_category1").append(a)
									.append(inputObj).append(label).append(inputObj1);
							childrenDiv.append(div);
						}
						childrenDiv.insertAfter(category_triggerObj.parent());
					}
				});
				
				//自身展开后，将其兄弟节点及其子节点收缩回
				category_triggerObj.parent(".tree_category1").next(".children").slideDown();
				category_triggerObj.parent(".tree_category1").siblings(".tree_category1").next(".children").slideUp(function(){
					 $(this).prev(".tree_category1").find(".category_trigger1").removeClass("close");
				 });
				 $("#productUnitPicker").html();
				return false;
			});
	
	$(".tree_btns1 .idBtnPicked").live('click', function() {
		var items = $("#sel_prodclass").find("[type='checkbox']");
		if(items != null){
			var str="";
			var pks="";
			for(var loop=0; loop<items.length; loop++){
				if(items[loop].checked == true){
					str += $(items[loop]).next("label").html() + ",";	
 					pks += $(items[loop]).val().substr(0,20) + ",";
				}
			}
			var length1=str.length;	
			var length2=pks.length;
			$("#qrygoodsClass").val(str.substring(0, length1-1));
			$("#pk_marbasclass").val(pks.substring(0, length2-1));
		}		
		$(this).parents(".ui-drop_dialog").slideUp();
		pkpubclassai();
		pkpubclassje();
		return 0;
	});
	
	$(".tree_btns1 .idBtnCancel").live('click', function(){
		$("#prodclass_filter").val("");
		$(this).parents(".ui-drop_dialog").slideUp();
	})

	function len(s) { 
		var l = 0; 
		var a = s.split(""); 
		for (var i=0;i<a.length;i++) { 
			if (a[i].charCodeAt(0)<299) { 
				l++; 
			} else { 
				l+=2; 
			} 
		} 
		return l; 
	}
}

$(function() {
	var gc = new GoodsClass();
});
